package com.gitee.fanx.rpc.client;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author fanhang
 */
public class RpcClientProxyFactoryBean implements FactoryBean<Object>, ApplicationContextAware {
	private final Class<?> clazz;
	private Object target;

	public RpcClientProxyFactoryBean(Class<?> clazz) {
		this.clazz = clazz;
	}

	@Override
	public Object getObject() {
		return target;
	}

	@Override
	public Class<?> getObjectType() {
		return clazz;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		RpcClientProxyFactory rpcClientProxyFactory = applicationContext.getBean(RpcClientProxyFactory.class);
		this.target = rpcClientProxyFactory.createClient(clazz);
	}
}
